package com.sp2.dev.other.xml;

/**
 * Created by IntelliJ IDEA.
 * User: zhimingli
 * Date: 14-3-3
 * Time: 上午11:37
 * To change this template use File | Settings | File Templates.
 */

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GetXml {
    public void BuildXMLDoc() throws IOException, JDOMException {
        // 创建根节点 list;
        Element root = new Element("urlset");

    

        System.out.println(root.getNamespace());


        // 根节点添加到文档中；
        Document doc = new Document(root);


        List<SiteMapVO> listSiteMap = new ArrayList<SiteMapVO>();
        for (int i = 0; i < 10; i++) {
            SiteMapVO vo = new SiteMapVO();
            vo.setId(i);
            vo.setLoc("loc" + i);
            vo.setPriority("priority" + i);
            vo.setType("type");
            listSiteMap.add(vo);
        }

        // 创建节点 users;

        for (int i = 0; i < listSiteMap.size(); i++) {
            Element urlElement = new Element("url");
            SiteMapVO vo = listSiteMap.get(i);
            urlElement.addContent(new Element("loc").setText(vo.getLoc()));
            urlElement.addContent(new Element("priority").setText(vo.getPriority()));
            root.addContent(urlElement);
        }

        XMLOutputter XMLOut = new XMLOutputter();

        // 输出 user.xml 文件；
        XMLOut.output(doc, new FileOutputStream("D:\\opt\\wikihtml\\sitemap\\user.xml"));
    }

    public static void main(String[] args) {
        try {
            GetXml x = new GetXml();
            System.out.println("生成 mxl 文件...");
            x.BuildXMLDoc();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
